﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace ElectronicCommerce.Framework.Data.Utility.LinqQeuryConditionBuilder
{
    public abstract class ExpressionBase : IExpression
    {
        #region Protected Members

        protected StringBuilder sql = new StringBuilder();
        protected readonly Dictionary<string, KeyValuePair<DbType, object>> parameters = new Dictionary<string, KeyValuePair<DbType, object>>();

        internal protected static string MakeUniqueParamNameWithoutPrefixToken()
        {
            return CommonUtils.MakeUniqueKey(15, "p");
        }

        #endregion

        #region IExpression Members

        public string Sql
        {
            get
            {
                return sql.ToString();
            }
            set
            {
                sql = new StringBuilder(value);
            }
        }

        public Dictionary<string, KeyValuePair<DbType, object>> Parameters
        {
            get
            {
                return parameters;
            }
        }

        public override string ToString()
        {
            return sql.ToString();
        }

        public string ToString(string aliasName)
        {
            sql.Insert(0, ".");
            sql.Insert(0, "[]");
            sql.Insert(1, aliasName);
            return sql.ToString();
        }

        #endregion
    }
}
